//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for GalleryConfiguration.
	/// </summary>
	[Serializable]
	public class GalleryConfiguration : ThemedApplicationKeyApplicationConfiguration
	{

		protected override string ThemeLocationDefault() { return "~/Themes/Galleries/"; }

		#region Private Members

		//Options for attachement storage are Local vs Database (* if internal allowed)
		//Internal vs External vs Either for attachemnet locations

		//Note default values must be also set in the static "Create Instance" method below
		//private bool enableFileSystemStorage = false;
		//private bool enableRemoteStorage = false;
		//private string fileSystemStorageLocation = "~/photos/storage";
		//private string cacheStorageLocation = "~/photos/cache";
		private bool createGalleryDirectories = true;
		private bool autoCreateGallery = false;
		private bool allowEncodedUnicodeCharsInMetadata = true;
		private int quality = 80;
		private int maxX = -1;
		private int maxY = -1;
		private int _imageQuota = -1;
		private int _diskQuota = -1;

		#endregion

		#region Public Properties

        private int _defaultGroupID = -1;
        public int DefaultGroupID
        {
            get { return _defaultGroupID; }
            set { _defaultGroupID = value; }
        }
        
	    public bool HasMaxX
		{
			get{return (MaxX != -1);}
		}
		public bool HasMaxY
		{
			get{return (MaxY != -1);}
		}
		public int MaxX
		{
			get { return maxX; }
			set
			{
				if(value < -1)
					value = -1;
				maxX = value;
			}
		}
		public int MaxY
		{
			get { return maxY; }
			set
			{
				if(value < -1)
					value = -1;
				maxY = value;
			}
		}
		public int Quality
		{
			get { return quality; }
			set { quality = value; }
		}

		public bool CreateGalleryDirectories
		{
			get{ return createGalleryDirectories; }
			set{createGalleryDirectories = value;}
		}

		public bool AutoCreateGallery
		{
			get{ return autoCreateGallery; }
			set{autoCreateGallery = value;}
		}

		public bool AllowEncodedUnicodeCharsInMetadata
		{
			get{ return allowEncodedUnicodeCharsInMetadata; }
			set{allowEncodedUnicodeCharsInMetadata = value;}
		}

		int _RssCacheWindowInSeconds = 90;
		public int RssCacheWindowInSeconds 
		{
			get 
			{
				return _RssCacheWindowInSeconds;
			}
			set 
			{
				_RssCacheWindowInSeconds = value;
			}
		}

		int _RssDefaultThreadsPerFeed = 20;
		public int RssDefaultThreadsPerFeed 
		{
			get 
			{
				return _RssDefaultThreadsPerFeed;
			}
			set 
			{
				_RssDefaultThreadsPerFeed = value;
			}
		}

		private int aggregatePostCount = 25;
		public int AggregatePostCount
		{
			get { return aggregatePostCount;}
			set {aggregatePostCount = value;}
		}

		public int DiskQuota
		{
			get { return _diskQuota; }
			set
			{
				if(value < -1)
					value = -1;
				_diskQuota = value;
			}
		}
		public int ImageQuota
		{
			get { return _imageQuota; }
			set
			{
				if(value < -1)
					value = -1;
				_imageQuota = value;
			}
		}

		#region Popular Posts

		int _PopularPostLevelPosts = 15;
		public int PopularPostLevelPosts 
		{
			get 
			{
                
				return _PopularPostLevelPosts;
			}
			set 
			{
				_PopularPostLevelPosts = value;
			}
		}

		int _PopularPostLevelViews = 200;
		public int PopularPostLevelViews 
		{
			get 
			{
                
				return _PopularPostLevelViews;
			}
			set 
			{
				_PopularPostLevelViews = value;
			}
		}
        
		int _PopularPostLevelDays = 3;
		public int PopularPostLevelDays 
		{
			get 
			{
                
				return _PopularPostLevelDays;
			}
			set 
			{
				_PopularPostLevelDays = value;
			}
		}
		#endregion

		#region Duplicate Posts
		bool _EnableDuplicatePosts = false;
		public bool EnableDuplicatePosts 
		{
			get 
			{
            
				return _EnableDuplicatePosts;
			}
			set 
			{
				_EnableDuplicatePosts = value;
			}
		}

		int _DuplicatePostIntervalInMinutes = 15;
		public int DuplicatePostIntervalInMinutes 
		{
			get 
			{
				return _DuplicatePostIntervalInMinutes;
			}
			set 
			{
				_DuplicatePostIntervalInMinutes = value;
			}
		}
		#endregion

		#region Post Editing
		bool _DisplayEditNotesInPost = false;
		public bool DisplayEditNotesInPost 
		{
			get 
			{
                
				return _DisplayEditNotesInPost;
			}
			set 
			{
				_DisplayEditNotesInPost = value;
			}
		}

		bool _RequireEditNotes = false;
		public bool RequireEditNotes 
		{
			get 
			{
				
				return _RequireEditNotes;
			}
			set 
			{
				_RequireEditNotes = value;
			}
		}

		int _PostEditBodyAgeInMinutes = 0;
		public int PostEditBodyAgeInMinutes 
		{
			get 
			{
                
				return _PostEditBodyAgeInMinutes;
			}
			set 
			{
				_PostEditBodyAgeInMinutes = value;
			}
		}

		int _PostEditTitleAgeInMinutes = 5;
		public int PostEditTitleAgeInMinutes 
		{
			get 
			{
                
				return _PostEditTitleAgeInMinutes;
			}
			set 
			{
				_PostEditTitleAgeInMinutes = value;
			}
		}

		int _PostDeleteAgeInMinutes = 5;
		public int PostDeleteAgeInMinutes 
		{
			get 
			{
				return _PostDeleteAgeInMinutes;
			}
			set 
			{
				_PostDeleteAgeInMinutes = value;
			}
		}
		#endregion



		#endregion

		private GalleryAttachmentSettings _fileSettings = null;
		private GalleryCacheSettings _cacheSettings = null;

		[ReadFromConfigurationFileOnly]
		public GalleryAttachmentSettings AttachmentSettings
		{
			get{ return _fileSettings;}
			set{_fileSettings=value;}
		}

		[ReadFromConfigurationFileOnly]
		public GalleryCacheSettings CacheSettings
		{
			get{ return _cacheSettings;}
			set{_cacheSettings=value;}
		}


		#region Public Methods

		public string GetFormattedDiskQuota()
		{
			return GetFormattedDiskQuota(this.DiskQuota);
		}

		public string GetFormattedDiskQuota(int diskQuota)
		{
			if(diskQuota < 0)
				return String.Empty;
			else if(diskQuota == 0)
				return diskQuota.ToString();
			else
				return (diskQuota / (int)DataUnit.KiloByte).ToString();
		}

		public string GetFormattedImageQuota()
		{
			return GetFormattedImageQuota(this.ImageQuota);
		}

		public string GetFormattedImageQuota(int imageQuota)
		{
			if(imageQuota < 0)
				return String.Empty;
			else
				return imageQuota.ToString();
		}

		public int ParseQuota(string quotaString, GalleryQuotaType quotaType)
		{
			int quota = -1;

			if(!Globals.IsNullorEmpty(quotaString))
			{
				try
				{
					quota = Int32.Parse(quotaString);
				}
				catch
				{
					quota = -1;
				}
				switch(quotaType)
				{
					case GalleryQuotaType.Disk:
						if(quota > 0)
						{
							quota *= (int)DataUnit.KiloByte;
						}
						else if(quota < 0)
						{
							quota = -1;
						}
						break;
					case GalleryQuotaType.Image:
						if(quota < 0)
							quota = -1;
						break;
					default:
						throw new ArgumentException("Quota type not expected.");
				}
			}

			return quota;
		}

		#endregion

		#region Public Static Methods

        private static readonly Type thisType = typeof(GalleryConfiguration);

        public static GalleryConfiguration Instance()
        {
            return Instance(true);
        }

		public static GalleryConfiguration Instance(bool useCache)
		{
            return Instance(thisType, useCache) as GalleryConfiguration;
		}

		#endregion

	    protected override string ConfigSection
	    {
	        get { return "CommunityServer/Gallery"; }
	    }

	    protected override void Initialize(XmlNode node)
	    {
            base.Initialize(node);
        

            if(node != null)
            {
                

                XmlAttributeCollection attributeCollection = node.Attributes;

                if(attributeCollection["enable"] != null)
                    this.Enabled = bool.Parse(attributeCollection["enable"].Value);

				if(attributeCollection["createGalleryDirectories"] != null)
                    this.createGalleryDirectories = Globals.SafeBool(attributeCollection["createGalleryDirectories"].Value, true );
					
                if(attributeCollection["allowEncodedUnicodeCharsInMetadata"] != null)
                    this.allowEncodedUnicodeCharsInMetadata = Globals.SafeBool(attributeCollection["allowEncodedUnicodeCharsInMetadata"].Value, true);

				if(attributeCollection["aggregatePostCount"] != null)
					this.aggregatePostCount = Int32.Parse(attributeCollection["aggregatePostCount"].ToString());
 
				XmlNode fs = node.SelectSingleNode("AttachmentSettings");
                this.AttachmentSettings =  Serializer.ConvertToObject(fs,typeof(GalleryAttachmentSettings)) as GalleryAttachmentSettings;

                XmlNode cs = node.SelectSingleNode("CacheSettings");
                this.CacheSettings =  Serializer.ConvertToObject(cs,typeof(GalleryCacheSettings)) as GalleryCacheSettings;
  
			}

            if(this.AttachmentSettings == null)
            {
                this.AttachmentSettings = new GalleryAttachmentSettings() ;
					
            }

            if(this.CacheSettings == null)
            {
                this.CacheSettings = new GalleryCacheSettings() ;
					
            }

	    }

	    public override ApplicationType ApplicationType
	    {
	        get { return ApplicationType.Gallery; }
	    }
	}
}
